﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
    public class OrderService
    {
        public List<Order> AllOrder = new List<Order>();

        //新增订单
        public Order AddOrder(Customer c, List<OrderDetail> details, Time time)
        {
            Order order = new Order(c, details, time);
            List<Order> orders = new List<Order>();
            orders.Add(order);
            if (Search(orders).Count != 0) throw new RepleteException("This order existed !!!!");
            AllOrder.Add(order);
            Order.number++;
            return order;
        }
        //删除订单
        public void RemoveOrder(List<Order> orders)
        {
            if (Search(orders).Count == 0) throw new NotFoundException("Order with this ID is not found!");
            Search(orders).ForEach(o => { AllOrder.Remove(o); Order.number--; });              
        }
        public void RemoveOrder(string ID)
        {
            if (Search(ID).Count == 0) throw new NotFoundException("Order with this ID is not found!");
            Search(ID).ForEach(o => { AllOrder.Remove(o); Order.number--; });
        }
        //修改订单

        public void modifyOrder(string ID, Customer c)
        {
            var query = AllOrder.Where(o => o.ID == ID);
            if (query.ToList().Count == 0) throw new NotFoundException("Order with this ID is not found!");
            if (query.ToList().Count > 1) throw new RepleteException("Order with this ID is more than one!");
            query.ToList()[0]=modify(query.ToList()[0],c);
        }
        public void modifyOrder(string ID, List<OrderDetail> details)
        {
            var query = AllOrder.Where(o => o.ID == ID);
            if (query.ToList().Count == 0) throw new NotFoundException("Order with this ID is not found!");
            if (query.ToList().Count > 1) throw new RepleteException("Order with this ID is more than one!");
            query.ToList()[0] = modify(query.ToList()[0], details);
        }

        public void modifyOrder(string ID, Time t)
        {
            var query = AllOrder.Where(o => o.ID == ID);
            if (query.ToList().Count == 0) throw new NotFoundException("Order with this ID is not found!");
            if (query.ToList().Count > 1) throw new RepleteException("Order with this ID is more than one!");
            query.ToList()[0] = modify(query.ToList()[0], t);
        }
        public Order modify(Order order, Customer c)
        {
            order.setCustomer(c);
            return order;
        }
        public Order modify(Order order, List<OrderDetail>details)
        {
            order = new Order(order.GetCustomer(), details, order.GetTime());
            return order;
        }
        public Order modify(Order order, Time t)
        {
            order.setTime(t);
            return order;
        }


        //查询订单
        public List<Order> Search(string[] orderID) {
            var query = AllOrder.Where(o => orderID.Contains(o.ID)).OrderBy(o => o.Amount);
            List<Order> list = query.ToList();
            return list;
        }
        public List<Order> Search(string orderID)
        {
            var query = AllOrder.Where(o => orderID==o.ID).OrderBy(o => o.Amount);
            List<Order> list = query.ToList();
            return list;
        }
        public List<Order> Search(OrderDetail detail)
        {
            var query = AllOrder.Where(o=> o.GetOrderDetails().Where(d=>d.goods.Name==detail.goods.Name).Count()==1).OrderBy(o => o.Amount);
            List<Order> list = query.ToList();
            return list;
        }
        public List<Order> Search(Customer c)
        {
            var query = AllOrder.Where(o => o.GetCustomer().getName().Equals(c.getName())).OrderBy(o => o.Amount);
            List<Order> list = query.ToList();
            return list;
        }

        public List<Order> Search(List<Order> orders)
        {
            var query = AllOrder.Where(o => orders.Contains(o)).OrderBy(o => o.Amount);
            List<Order> list = query.ToList();
            return list;
        }
        //查找所有总金额超过某个金额的订单
        public List<Order>Search(double amount1)
        {
            var query = AllOrder.Where(o => o.Amount-amount1>=0).OrderBy(o => o.Amount);
            List<Order> list = query.ToList();
            return list;
        }
        
        //订单按订单ID排序或自定义排序

        public void Sort()
        {
            AllOrder.Sort((s1, s2) => s1.ID.CompareTo(s2.ID));
        }

        public void Sort(IComparer<Order>comparer)
        {
            AllOrder.Sort(comparer);
        }
        
    }
    public class NotFoundException : Exception
    {
        public NotFoundException(string message):base(message)
        {
        }
    }

    public class RepleteException : Exception
    {
        public RepleteException(string message) : base(message)
        {

        }
    }
}
